// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "category_id": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "goods_sn": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "货品编码",
    "label": "货品编码"
  },
  "order_number": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      }
    ],
    "title": "订购数量",
    "label": "订购数量"
  },
  "name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "货品名称",
    "label": "货品名称"
  },
  "type": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "规格型号",
    "label": "规格型号"
  },
  "code": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "物料编码",
    "label": "物料编码"
  },
  "danwei": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "计量单位",
    "label": "计量单位"
  },
  "unit_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "供货商",
    "label": "供货商"
  },
  "keywords": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "关键字",
    "label": "关键字"
  },
  "goods_desc": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "产品中文描述",
    "label": "产品中文描述"
  },
  "goods_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "订货日期",
    "label": "订货日期"
  },
  "yj_goods_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "预计发货日期",
    "label": "预计发货日期"
  },
  "add_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "defaultValue": {
      "$env": "now"
    }
  },
  "last_modify_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "defaultValue": {
      "$env": "now"
    }
  },
  "seller_note": {
    "rules": [
      {
        "format": "string"
      }
    ]
  }
}

const enumConverter = {}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
